<?php

	/**
	 * Crawls wine item from wines.com
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class WinesCom implements WineSource
	{
		/* VARIABLES */
		
		/**
		 * Beer list base URL
		 *
		 * @var string
		 */
		public $listUrl = '';
		
		/**
		 * Beer item base URL
		 *
		 * @var string
		 */
		public $itemUrl = 'http://www.wines.com/master_wine_search?wine_search[master_wine_id]=*';
		
		/**
		 * Wine URLs
		 *
		 * @var array
		 */
		public $wineURLs;
		
		
		/* CONSTRUCTORS */
			
		/**
		 * Defualt constructor
		 *
		 */
		public function WinesCom()
		{
			
		}		
		
		
		/* METHODS */			
				
		/**
		 * Gets wine URLs list
		 *
		 * @param int $fromRecord offset
		 * @param int $toRecord count
		 */
		public function GetSavedWineURLs($fromRecord = '', $toRecord = '')
		{
			for ($i = $fromRecord + 1; $i <= $fromRecord + $toRecord; $i++) 
			{
				$urls[$i]['id'] = $i;
				$urls[$i]['url'] = str_replace('*', $i, $this->itemUrl);
			}
			
			return $urls;
		}						
		
		/**
		 * Get uncrawled wine URLs
		 *
		 */
		public function GetUncrawledWineURLs()
		{
			$uncrawledURLs = DB::ListItems("Select * from t_wines_3 where t_wines_3.name = ''");
			$urls = array();
			
			foreach ($uncrawledURLs as $i => $uncrawledURL)
			{
				$urls[$i]['id'] = $uncrawledURL['wine_id'];
				$urls[$i]['url'] = $uncrawledURL['web_address'];
			}
			
			return $urls;			
		}
		
		/**
		 * Gets wine item
		 * 
		 * @param int $id wine id
		 * 
		 * @return WineItem
		 *
		 */
		public function GetWineItem($url)
		{
			//landing page
			$landingPage = phpQuery::newDocumentFileHTML($url);

			//wine page URL
			$winePageURL = $landingPage->find(".result_info > h5 > a")->attr('href');
						
			//if no wine detected
			if (empty($winePageURL))
			{
				return null;
			}
			
			//wine page
			$winePage = phpQuery::newDocumentFileHTML($winePageURL);			
			
			//wine info
			$wineInfo = $winePage->find(".column.grid-4.left_col")->children()->get(1);
			
			//wine item
			$wineItem = new WineItem();
			
			//display name
			$wineItem->displayName = trim($winePage->find(".column.grid-6.center_col > .panel.top > h3")->html());
			
			//year / name
			if (ereg("([0-9]{4})", $wineItem->displayName, $matches))
			{
				$wineItem->vintage = $matches[1];
				$pos = strpos($wineItem->displayName, $wineItem->vintage);
				$wineItem->name = trim(substr($wineItem->displayName, 0, $pos));
			}
			else 
			{
				$wineItem->vintage = 'NULL';
				$wineItem->name = $wineItem->displayName;
			}			
			
			//other details
			$wineInfoText = pq($wineInfo)->html();
			$detailsParts = explode('<br>', $wineInfoText);
			foreach ($detailsParts as $detail)
			{
				$detailParts = explode(':', trim($detail));
				switch (trim(strip_tags($detailParts[0])))
				{
					case 'Winery':
						$wineItem->producer = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Country':
						$wineItem->country = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Region':
						$wineItem->region = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Subregion':
						$wineItem->subRegion = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Appellation':
						$wineItem->appelation = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Varietal':
						$wineItem->varietal = trim(strip_tags($detailParts[1]));
						break;
						
					case 'Type':
						$wineItem->type = trim(strip_tags($detailParts[1]));
						break;
						
					default:
						break;	
				}
			}
			
			//photo
			$wineItem->photoURL = $winePage->find("#wine_images > img")->attr('src');
			
			//URL
			$wineItem->webAddress = $winePageURL;
			
			//common details
			$wineItem->urlId = 0;
			$wineItem->source = 'wines.com';
			$wineItem->tableName = 't_wines_3';

			return $wineItem;
		}
		
		/**
		 * Gets wine image
		 * 
		 * @param int $id beer id
		 * 
		 * @return BeerItem
		 *
		 */
		public function GetWineItemImage($beerId)
		{
			//parsed page
			$page = phpQuery::newDocumentFileHTML(str_replace('*', $beerId, $this->itemUrl));
			
			//image url
			$image = $page->find(".project-details > img")->attr('src');

			return (!empty($image)) ? 'http://www.brewerydb.com'.$image : '';
		}		
		
	}

?>